import Promise from 'es6-promise-polyfill'

window.Promise = window.Promise || Promise;

import Vue from 'vue'
import Vuex from 'vuex'
import global from './global'

Vue.use(Vuex)

var store = null, interval = null
try {
    store = new Vuex.Store({
        state: {
            username: '',
            title: '',
            logs: [],
            customer: null,
            admin: null,
            code_countdown: 0,
            //toast
            toast: {
                is_show: false,
                type: '',
                content: ''
            },
            //loading
            loading: {
                is_show: false,
                content: ''
            },
            //dialog
            login_dialog: {
                is_show: false
            },
            dialog: {
                is_show: false,
            },
        },
        mutations: {
            login(state, username) {
                state.username = username
                global.ws = new WebSocket(global.webSocketServer);
                var ws = global.ws
                ws.onopen = function () {
                };
                ws.onmessage = evt => {
                    var svcn = evt.data.split(' ')[0]// short for service name
                    switch (svcn) {
                        case 'log':
                            var logs = JSON.parse(evt.data.substring(svcn.length + 1))
                            state.logs.push.apply(state.logs, logs)
                            break
                        case 'delLog':
                            state.logs.splice(evt.data.substring(svcn.length + 1), 1)
                    }
                };
            },
            setTitle(state, title) {
                state.title = title
            },
            set_title(state, title) {
                state.title = title
            },
            customer_login(state, obj) {
                if (!obj && !state.customer) {
                    for (var key in obj) {
                        obj[key] = obj[key] || state.customer[key]
                    }
                }
                state.customer = obj
            },
            admin_login(state, obj) {
                if (obj == null) {
                    state.admin = null
                } else {
                    if (!state.admin)
                        state.admin = {}
                    for (var key in obj) {
                        state.admin[key] = obj[key]
                    }
                }
            },
            code_countdown(state) {
                if (interval == null) {
                    state.code_countdown = 60;
                    interval = setInterval(() => {
                        if (state.code_countdown > 0) {
                            state.code_countdown = state.code_countdown - 1;
                        } else {
                            clearInterval(interval)
                            interval = null
                        }
                    }, 1000)
                }
            },
            toast(state, obj) {
                if (typeof obj.is_show == 'undefined')
                    obj.is_show = true
                state.toast.is_show = obj.is_show;
                state.toast.type = obj.type || '';
                state.toast.content = obj.content || '';
                if (state.toast.is_show) {
                    setTimeout(() => {
                        state.toast.is_show = false;
                        state.toast.type = '';
                        state.toast.content = '';
                    }, 3000)
                }
            },
            loading(state, obj) {
                state.loading = obj
            },
            login_dialog(state, obj) {
                state.login_dialog = obj
            },
            dialog(state, obj) {
                state.dialog = obj
            },
        }
    })
} catch (err) {
    alert(err)
}

export default store
